#include "video.h"



bool Video::splitVideoByFixedFrames(const std::string &inputVideoPath, const std::string &outputVideoPrefix, int framesPerSegment)
{
    cv::VideoCapture cap(inputVideoPath);
    if (!cap.isOpened()) {
        qDebug() << "无法打开输入视频文件: " << QString::fromStdString(inputVideoPath);
        return false;
    }

    int frameWidth = static_cast<int>(cap.get(cv::CAP_PROP_FRAME_WIDTH));
    int frameHeight = static_cast<int>(cap.get(cv::CAP_PROP_FRAME_HEIGHT));
    double fps = cap.get(cv::CAP_PROP_FPS);

    int frameCount = 0;
    int segmentIndex = 0;
    cv::VideoWriter writer;

    cv::Mat frame;
    while (cap.read(frame)) {
        if (frameCount % framesPerSegment == 0) {
            if (writer.isOpened()) {
                writer.release();
            }
            std::string outputVideoPath = outputVideoPrefix  + std::to_string(segmentIndex) + ".mp4";
            writer.open(outputVideoPath, cv::VideoWriter::fourcc('X', '2', '6', '4'), fps, cv::Size(frameWidth, frameHeight));
            if (!writer.isOpened()) {
                qDebug() << "无法创建输出视频文件: " << QString::fromStdString(outputVideoPath);
                cap.release();
                return false;
            }
            segmentIndex++;
            if(m1.lock())
            {
                int data;
                memcpy(&data,m1.data(),sizeof(int));
                data++;
                qDebug()<<"wt1"<<data;
                memcpy(m1.data(),&data,sizeof(int));
                m1.unlock();
            }

        }
        writer.write(frame);
        frameCount++;

    }

    if (writer.isOpened()) {
        writer.release();
    }
    cap.release();
    return true;
}
